try {
	Type.registerNamespace('NCMMPUINUtil');

	NCMMPUINUtil.getAddScanedNum = function(arg1, arg2) {

		var r1,
		    r2,
		    m;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch(e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch(e) {
			r2 = 1
		}
		m = Math.pow(10, Math.max(r1, r2));
		return (arg1 * m + arg2 * m) / m;
	}
	NCMMPUINUtil.getAddNum = function(arg1, arg2) {
		var r1,
		    r2,
		    m;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch(e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch(e) {
			r2 = 0
		}
		m = Math.pow(10, Math.max(r1, r2));
		return (arg1 * m + arg2 * m) / m;
	}

	NCMMPUINUtil.getSubScanedNum = function(arg1, arg2) {
		var r1,
		    r2,
		    m,
		    n;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch(e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch(e) {
			r2 = 1
		}
		m = Math.pow(10, Math.max(r1, r2));
		//动态控制精度长度
		n = (r1 >= r2) ? r1 : r2;
		return ((arg1 * m - arg2 * m) / m).toFixed(n);
	}
	NCMMPUINUtil.getSubNum = function(arg1, arg2) {

		var r1,
		    r2,
		    m,
		    n;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch(e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch(e) {
			r2 = 0
		}
		m = Math.pow(10, Math.max(r1, r2));
		//动态控制精度长度
		n = (r1 >= r2) ? r1 : r2;
		//$alert("m="+m.toString+" n="+n.toString()+" arg1-arg2="+((arg1*m-arg2*m)/m).toString());
		return ((arg1 * m - arg2 * m) / m).toFixed(n);
	}
	/**
	 *num1>num2 返回true;否则返回false
	 */
	NCMMPUINUtil.isBigger = function(num1, num2) {
		if (num1 == null || num1 == "") {
			num1 += "00.00000";
		} else {
			num1 = parseFloat(num1);
		}
		if (num2 == null || num2 == "") {
			num2 += "00.00000";
		} else {
			num2 = parseFloat(num2);
		}
		var rtn = (num1 - num2) > 0 ? true : false;
		return rtn;
	}
	/**
	 *计算主数量 
	 */
	NCMMPUINUtil.getNum = function(nastnum, vchange) {
	    var vchange1=1;//分子
	    var vchange2=1;//分母
		if(nastnum == null || nastnum == "") {
			nastnum += "00.00000";
		} else {
			nastnum = parseFloat(nastnum);
		}
		if(nastnum==0){
		return 0;
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
		    vchange1=parseFloat(vchange.split("/")[0]);
		    vchange2=parseFloat(vchange.split("/")[1]);
		}
		//主数量=辅数量*换算率=辅数量*分子/分母
		var num = nastnum.mul(vchange1).div(vchange2);
		return num;
	}
	/**
	 *主数量除以换算率得辅数量 
	 * @param {Object} nnum
	 * @param {Object} vchange
	 */
	NCMMPUINUtil.getNastum = function(nnum, vchange) {
	    var vchange1=1;//分子
	    var vchange2=1;//分母
		if(nnum == null || nnum == "") {
			nnum += "00.00000";
		} else {
			nnum = parseFloat(nnum);
		}
		if(nnum==0){
		return 0;
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
			vchange1=parseFloat(vchange.split("/")[0]);
		    vchange2=parseFloat(vchange.split("/")[1]);
		}
		//辅数量=主数量/换算率=主数量*分母/分子
		var nastum = nnum.mul(vchange2).div(vchange1);
		return nastum;
	}

	NCMMPUINUtil.getCanInNum = function(colpk, num, ninnum) {
		var where = " where pkarriveorderb='" + colpk + "'";
		var scanednum = NCMMPubUtil.queryVORtnOneCol(PurchaseInVO, PurchaseInVO.SCANEDNUM, where);

		if (NCMMPubUtil.isEmpty(scanednum) || $jsonToString(scanednum) == "[]" || scanednum[0]["scanednum"] == "") {
			return num;
		} else {
			var nscannum = scanednum[0]["scanednum"];
			var num = NCMMPUINUtil.getSubNum(num, nscannum);
			if (ninnum != 0) {
				num = NCMMPUINUtil.getAddNum(num, ninnum);
			}
			return num;
		}
	}
	/**
	 *可入库数量=数量-已扫描数量
	 * @param {Object} nnum
	 * @param {Object} vchange
	 */
	NCMMPUINUtil.getCanInastnum = function(nnum, scanednum, vchange) {
		if (nnum == null || nnum == "") {
			nnum += "00.00000";
		} else {
			nnum = parseFloat(nnum);
		}
		if (scanedNum == null || scanedNum == "") {
			scanedNum += "00.00000";
		} else {
			scanedNum = parseFloat(scanedNum);
		}
		var caninnum = nnum - scanedNum;
		return NCMMPUINUtil.getNastum(caninnum, vchange);
	}
	/**
	 *如果小数点位数多余某位，则四舍五入截断为bitnum位
	 */
	NCMMPUINUtil.decimalPrecision = function(num, bitnumber) {
		var rtn = {
			"flag" : false
		};
		var floatNum = parseFloat(num);
		if (isNaN(floatNum) || isNaN(bitnumber)) {
			return rtn;
		}
		if (bitnumber == 0) {
			rtn["flag"] = true;
			rtn["num"] = parseInt(floatNum);
			return rtn;
		}
		bitnumber = parseFloat(bitnumber);
		//实现四舍五入 比如两个精度 先乘以100再除以100
		var t = 1;
		var tmpbitnumber = bitnumber;
		for (; tmpbitnumber > 0; t *= 10, tmpbitnumber--);
		floatNum = Math.round(floatNum * t) / t;
		rtn["flag"] = true;
		rtn["num"] = floatNum;
		return rtn;
	}
	NCMMPUINUtil.isBitEnByBitNum = function(num, bitnum) {
		var rtn = false;
		var stringNum = num.toString();
		var bitNumberIndex = stringNum.indexOf('.');
		//整数
		if (bitNumberIndex < 0) {
			if (bitnum >= 0) {
				rtn = true;
			}
		} else if ((stringNum.length - 1 - bitNumberIndex) <= bitnum) {
			rtn = true;
		}
		return rtn;
	}

	NCMMPUINUtil.isBitEn = function(num, cunitid) {
		var rtn = false
		if (NCMMPubUtil.isEmpty(num) || NCMMPubUtil.isEmpty(cunitid)) {
			return rtn;
		}
		//1 根据cunitd查询单位vo获得bitnumber
		var bitNumberJson = NCMMPUINUtil.getBitnumber(cunitid);
		if (bitNumberJson["flag"]) {
			rtn = NCMMPUINUtil.isBitEnByBitNum(num, bitNumberJson["bitnumber"]);
		}
		// 如果单位档案未下载，则需要判断位数不超过8即可
		else {
			rtn = NCMMPUINUtil.isBitEnByBitNum(num, 8);
		}
		return rtn;
	}
	/**
	 *根据单位id计算num的精度
	 * @param {Object} num
	 * @param {Object} cunitid
	 */
	NCMMPUINUtil.calNumByBitnumber = function(num, cunitid) {
		var rtn = {
			"flag" : false
		};
		if (NCMMPubUtil.isEmpty(num) || NCMMPubUtil.isEmpty(cunitid)) {

			return rtn;
		}
		//1 根据cunitd查询单位vo获得bitnumber
		var bitNumberJson = NCMMPUINUtil.getBitnumber(cunitid);
		if (!bitNumberJson["flag"]) {
			//特殊处理一下位数超过8位的 四舍五入为8位
			var newrtn = NCMMPUINUtil.decimalPrecision(num, 8);
			return newrtn;
		}

		//2 根据bitnumber计算数量
		var numJson = NCMMPUINUtil.toDecimalN(num, bitNumberJson["bitnumber"]);

		if (numJson["flag"]) {
			rtn["flag"] = true;
			rtn["bitnumber"] = bitNumberJson["bitnumber"];
			rtn["cunitid"] = cunitid;
			rtn["num"] = numJson["num"];
		}

		return rtn;
	}
	/**
	 *保留num的bitnumber位小数 不足时补0
	 */
	NCMMPUINUtil.toDecimalN = function(num, bitnumber) {
		var rtn = {
			"flag" : false
		};
		var floatNum = parseFloat(num);
		if (isNaN(floatNum) || isNaN(bitnumber)) {
			return rtn;
		}
		if (bitnumber == 0) {
			rtn["flag"] = true;
			rtn["num"] = parseInt(floatNum);
			return rtn;
		}
		bitnumber = parseFloat(bitnumber);
		//实现四舍五入 比如两个精度 先乘以100再除以100
		var t = 1;
		var tmpbitnumber = bitnumber;
		for (; tmpbitnumber > 0; t *= 10, tmpbitnumber--);
		floatNum = Math.round(floatNum * t) / t;
		//实现位数不足时 补零
		var stringNum = floatNum.toString();
		var bitNumberIndex = stringNum.indexOf('.');

		if (bitNumberIndex < 0) {
			bitNumberIndex = stringNum.length;
			stringNum += '.';
		}
		while (stringNum.length <= bitNumberIndex + bitnumber) {
			stringNum += '0';
		}
		rtn["flag"] = true;
		rtn["num"] = stringNum;
		return rtn;
	};
	/**
	 * 根据单位id查询
	 * @param {Object} cunitid
	 */
	NCMMPUINUtil.getBitnumber = function(cunitid) {
		var rtn = {
			"flag" : false
		};
		if (NCMMPubUtil.isEmpty(cunitid)) {
			return rtn;
		}
		var where = " where pk_measdoc='" + cunitid + "'";
		var data = NCMMPubUtil.queryVONoCallback(MeasdocVO, where);
		if (NCMMPubUtil.isNotEmpty(data)) {
			var measdocvo = eval(data);
			// vos[0] = NCMMPubUtil.htmlspecialjson2UI(vos[0]);
			if (NCMMPubUtil.isNotEmpty(measdocvo[0])) {
				rtn["flag"] = true;
				rtn["bitnumber"] = measdocvo[0]["bitnumber"];
				rtn["cunitid"] = cunitid;
			}
		}
		return rtn;
	};
	//调用扩展服务提供的NC数据下载
	//param:
	//VOClass：数据VO
	//appbillname：档案/单据名称
	//condition：过滤条件
	//callback：事件结束后的回调方法，由调用js提供
	//needReloadrow：是否需要刷新界面，如果需要，刷新方法由调用js提供
	NCMMPUINUtil.processBillDataFromNC = function(VOClass, appbillname, condition, callback) {
		$js.showLoadingBar({
			msg : "正在连接服务器..."
		});
		var host = $cache.read('host');
		var port = $cache.read('port');
		$app.checkNetWorkReachable(host, port, function(param, args) {
			if (args.is_reachable == 'true') {
				$app.showLoadingBar({
					msg : "正在下载……"
				});
				NCMMDBUtil.loadNCBaseData(PurchaseInVO, PurchaseInVO, NCMMHttpUtil.BILL, condition, function(data) {
					//如果没有从NC端下载到数据，则本地记录不更新
					if (data == null) {
						$js.hideLoadingBar();
						$toast("没有获取到符合条件的订单");
						return;
					}
					if (data != null && typeof (data) == 'string') {
						$js.hideLoadingBar();
						$toast("下载失败：" + data);
						return;
					}
					//										$alert("process data=" + $jsonToString(data));
					// 如果没有信息直接返回
					var billinfos = data.billinfos || {};
					var props = Object.getOwnPropertyNames(billinfos) || [];
					if (props.length == 0) {
						if ($isJSONObject(data)) {
							$js.hideLoadingBar();
							$toast("没有下载到【" + appbillname + "】");
						} else {
							$js.hideLoadingBar();
							$toast(data);
						}
						return;
					}
					// 清空本地表记录
					NCMMDBUtil.clearAllOldData(VOClass, null);
					// 把NC数据拉平
					billinfos_local = NCMMPUINUtil.parseNCData2Local(billinfos);
					//					$alert("billinfos_local=" + $jsonToString(billinfos_local));
					// TODO 执行拓展服务insert操作
					var attrs = VOClass.prototype.getAllAttrs();
					var fields = attrs.map(function(row, i, arr) {
						return row.name;
					});

					var valueMapList = Array.prototype.map.call(billinfos_local, function(row, i, arr) {
						var billinfo_local = $stringToJSON((row || {}));
						var values = attrs.map(function(attr, i, array) {
							var fname = attr.name;
							if (!!billinfo_local[fname]) {
								if (billinfo_local[fname] != "" && typeof (billinfo_local[fname]) == "string") {
									billinfo_local[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(billinfo_local[fname]);
								}
								return billinfo_local[fname];
							} else {
								if (attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
									return 0;
								} else if (attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
									return 0.0;
								} else {
									return "";
								}
							}
						});
						return values;
					});

					var sum = valueMapList.length;
					//					$alert("valueMapList=" + $jsonToString(valueMapList));

					$app.replaceBatch({
						db : NCMMPubUtil.DatabaseName,
						table : VOClass.prototype.getTableName(),
						fields : fields,
						values : valueMapList,
						callback : function(args_null, args) {
							if (args.code == '1000' || args.code == 1000) {
								$js.hideLoadingBar();
								$toast("【" + appbillname + "】下载了【" + sum + "】条数据。");
								if (callback) {
									callback(sum);
								}
							} else {
								$js.hideLoadingBar();
								$js.toast("【" + appbillname + "】下载异常");
								if (callback) {
									callback(null);
								}
							}
						}
					});

				});

			} else {
				$js.hideLoadingBar();
				$js.toast("下载失败请检查网络设置！");
			}
		});

	};
	NCMMPUINUtil.parseNCData2Local = function(billinfos) {
		var billinfos_local = [];
		for (var i = 0; i < billinfos.length; i++) {
			var billinfo = (billinfos[i]).billinfo;
			for (var j = 0; j < (billinfo.items).length; j++) {
				var temp = {};
				$.extend(temp, billinfo.head, ((billinfo.items)[j]).item);
				temp = processLocalScannum(temp);
				billinfos_local.push(temp);
			}
		}
		return billinfos_local;
	};

	NCMMPUINUtil.inserAggVO = function(VOHClass, vos) {
		var rtns = [];
		var billinfos = vos.billinfos;
		for (var bis in billinfos) {
			var items = bis.billinfo.items;
			for (var its in items) {
			}
		}
		return rtns;
	};
	NCMMPUINUtil.getDefaultStockPK = function() {
		var stockorg = $stringToJSON($cache.read("defaultstockorg"));
		if (stockorg == null || stockorg == "")
			return "";
		return stockorg.pk_stockorg;
	};
	NCMMPUINUtil.getDefaultStordocPK = function() {
		var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if (stordoc == null || stordoc == "")
			return "";
		return stordoc.pk_stordoc;
	}
	NCMMPUINUtil.getDefaultStordocName = function() {
		var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if (stordoc == null || stordoc == "")
			return "";
		return stordoc.name;
	}
	   //处理下单引号
    NCMMPUINUtil.htmlsqmchars2UI = function(s) {
        if(s == null || s == NaN)
            return s;

        if(typeof(s) == "string" && s != "") {
            s = s.replace(/[']?/mg, function(tokens, index, currentStr) {
                if(tokens.charCodeAt(0) == 39) {
                    return tokens + tokens;
                }
                return tokens;
            });
        }

        return s;
    };
	NCMMPUINUtil.showMenuLists = function(controlid) {
		$menu.openDropDownList({
			"controlid" : controlid,
			"dropDownListWidth" : "100",
			"background" : "#f2f2f2",
			"color" : "#ee4311",
			"font-size" : "12",
			"halign" : "center",
			"split-color" : "#ee4311",
			"panelstyle" : "round-div",
			"border-color" : "#ee4311",
			//"showtype": "right",
			"dropItemsArray" : [{
				"name" : "主界面",
				"action" : "showMain()"
			}, {
				"name" : "数据管理",
				"action" : "showDateManager()"
			}, {
				"name" : "业务设置",
				"action" : "businessSetting()"
			}, {
				"name" : "注销",
				"action" : "loginoff()"
			}, {
				"name" : "扫描记录",
				"action" : "showScanInfo()"
				//}, {
				//	"name": "Test",
				//	"action": "showScanImg()"
			}]
		})
	};
NCMMPUINUtil.showMenuLists1 = function(controlid) {
		$menu.openDropDownList({
			"controlid" : controlid,
			"dropDownListWidth" : "100",
			"background" : "#f2f2f2",
			"color" : "#ee4311",
			"font-size" : "12",
			"halign" : "center",
			"split-color" : "#ee4311",
			"panelstyle" : "round-div",
			"border-color" : "#ee4311",
			//"showtype": "right",
			"dropItemsArray" : [{
				"name" : "主界面",
				"action" : "showMain()"
			}, {
				"name" : "数据管理",
				"action" : "showDateManager()"
			}, {
				"name" : "业务设置",
				"action" : "businessSetting()"
			}, {
				"name" : "注销",
				"action" : "loginoff()"
			}, {
				"name" : "扫描记录",
				"action" : "showScanInfo1()"
				//}, {
				//	"name": "Test",
				//	"action": "showScanImg()"
			}]
		})
	};
	function showMain() {
		$view.open({
			"viewid" : "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名
			"isKeep" : "true" //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		})
	}

	function showDateManager() {
		// 跳转到数据管理界面
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.DM_main",
			isKeep : "true" //
		})
	}

	function businessSetting() {
		// 跳转到业务设置界面
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.BusinessSettingPage",
			isKeep : "true" //
		})
	}

	function loginoff() {
		// 跳转到登录界面
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.LoginPage", //主界面id
			isKeep : "false" //
		})
	}

	function showScanInfo() {
		// 跳转到扫描记录
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.PurchaseInScanListPage", //主界面id
			isKeep : "false" //
		})
	}
function showScanInfo1() {
		// 跳转到扫描记录
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.PurchaseInScanListPage", //主界面id
			isKeep : "true" //
		})
	}
	function showScanImg() {
		// 生成二维码
		$view.open({
			viewid : "com.yonyou.ncmm_pac_pda.PuInTest", //主界面id
			isKeep : "false" //
		})
	}
	

} catch(e) {
	$e(e);
}